/*
 * Copyright (c) 2011 Ayan Dave http://www.linkedin.com/in/daveayan
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
 * associated documentation files (the "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
 * following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial 
 * portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package testrj.domain;

import java.math.BigDecimal;
import java.util.List;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

public class TestrjUtils {

	public static Object transformObjectTo(Object object, String toClass) {
		if(toClass.equals("null")) {
			return null;
		} else if(toClass.equals("java.lang.Integer")) {
			return Integer.parseInt(object.toString());
		} else if(toClass.equals("java.lang.Double")) {
			return Double.parseDouble(object.toString());
		} else if(toClass.equals("java.lang.Boolean")) {
			return Boolean.parseBoolean(object.toString());
		} else if(toClass.equals("java.math.BigDecimal")) {
			return new BigDecimal(object.toString());
		} else if(toClass.equals("java.lang.Float")) {
			return Float.parseFloat(object.toString());
		} 
		return object;
	}
	
	public static String getReferenceNameFor(MethodToTest methodToTest) {
		return getReferenceNameFor(methodToTest.getObjectUnderTest(), methodToTest.getMethodName(), methodToTest.currentTestCase().getInputParams());
	}

	public static String getReferenceNameFor(String className, String methodName, List<DomainObject> inputParams) {
		return getReferenceNameFor(className, methodName, classNamesAsString(inputParams));
	}

	public static String getReferenceNameFor(String className, String methodName, Object[] inputParams) {
		return getReferenceNameFor(className, methodName, classNamesAsString(inputParams));
	}

	public static String getReferenceNameFor(String className, String methodName, String classNamesAsString) {
		return className + "/" + methodName + "-" + classNamesAsString;
	}

	public static List<String> getListOfParameterClassAsString(List<DomainObject> domainObjects) {
		return Lists.transform(domainObjects, new Function<DomainObject, String>() {
			public String apply(DomainObject object) {
				return object.className();
			}
		});
	}

	public static String classNamesAsString(Object[] inputParams) {
		StringBuffer sb = new StringBuffer();
		for (Object object : inputParams) {
			sb.append("_");
			if (object != null) {
				sb.append(object.getClass().getName());
			}
		}
		return sb.toString();
	}

	public static String classNamesAsString(List<DomainObject> inputParams) {
		StringBuffer sb = new StringBuffer();
		for (DomainObject object : inputParams) {
			sb.append("_");
			if (object != null) {
				sb.append(object.className());
			}
		}
		return sb.toString();
	}
}